package com.lineage.server.utils;

import com.lineage.config.Config;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BinaryOutputStream extends OutputStream {
  private static final String CLIENT_LANGUAGE_CODE = Config.CLIENT_LANGUAGE_CODE;
  
  private final ByteArrayOutputStream _bao = new ByteArrayOutputStream();
  
  public void write(int b) throws IOException {
    this._bao.write(b);
  }
  
  public void writeD(int value) {
    this._bao.write(value & 0xFF);
    this._bao.write(value >> 8 & 0xFF);
    this._bao.write(value >> 16 & 0xFF);
    this._bao.write(value >> 24 & 0xFF);
  }
  
  public void writeH(int value) {
    this._bao.write(value & 0xFF);
    this._bao.write(value >> 8 & 0xFF);
  }
  
  public void writeC(int value) {
    this._bao.write(value & 0xFF);
  }
  
  public void writeP(int value) {
    this._bao.write(value);
  }
  
  public void writeL(long value) {
    this._bao.write((int)(value & 0xFFL));
  }
  
  public void writeF(double org) {
    long value = Double.doubleToRawLongBits(org);
    this._bao.write((int)(value & 0xFFL));
    this._bao.write((int)(value >> 8L & 0xFFL));
    this._bao.write((int)(value >> 16L & 0xFFL));
    this._bao.write((int)(value >> 24L & 0xFFL));
    this._bao.write((int)(value >> 32L & 0xFFL));
    this._bao.write((int)(value >> 40L & 0xFFL));
    this._bao.write((int)(value >> 48L & 0xFFL));
    this._bao.write((int)(value >> 56L & 0xFFL));
  }
  
  public void writeS(String text) {
    try {
      if (text != null)
        this._bao.write(text.getBytes(CLIENT_LANGUAGE_CODE)); 
    } catch (Exception exception) {}
    this._bao.write(0);
  }
  
  public void writeByte(byte[] text) {
    try {
      if (text != null)
        this._bao.write(text); 
    } catch (Exception exception) {}
  }
  
  public int getLength() {
    return this._bao.size() + 2;
  }
  
  public byte[] getBytes() {
    return this._bao.toByteArray();
  }
}
